﻿using Leo.Blog.Common.LogHelper;
using Leo.Blog.Filter;
using Leo.Blog.ViewModel.SysModule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Leo.Blog.Controllers
{
    public class BaseController : Controller
    {
        private int _userId = 0;
        public int UserId
        {
            get
            {
                return _userId;
            }
            set
            {
                this._userId = value;
            }
        }
        private string _userName = string.Empty;
        public string UserName
        {
            get
            {
                return _userName;
            }
            set
            {
                this._userName = value;
            }
        }
        private string _userAccount = string.Empty;
        public string UserAccount
        {
            get
            {
                return _userAccount;
            }
            set
            {
                this._userAccount = value;
            }
        }
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
        }
        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            UserAccountVM accountVM = Session["UserAccount"] as UserAccountVM;
            if (accountVM==null)
            {
                filterContext.Result = new RedirectResult("/Login/Index");
            }
            else
            {
                
                _userId = accountVM.UserId;
                _userAccount = accountVM.Account;
                _userName = accountVM.UserName;

                var targetType = this.GetType();
                var actionName = filterContext.ActionDescriptor.ActionName;//获取执行的action名字
                var methodInfo = targetType.GetMethod(actionName);
                if (methodInfo!=null&&methodInfo.IsDefined(typeof(AllowAccessAttribute), false))
                {
                    return;
                }

                List<UserModule> listModule = accountVM.Modules;
                if (Request.Url != null)
                {
                    string url = Request.Url.LocalPath;
                    if (url != "/" && !url.Contains("Home") 
                        && !url.Contains("Error") 
                        && !listModule.Any(u => url.Contains(u.WebUrl))
                        && !Request.IsAjaxRequest())
                    {
                        filterContext.Result = new RedirectResult("/Error/NoAccess");
                        return;
                    }
                }
            }
            base.OnAuthorization(filterContext);
        }

        protected override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);

            //获取异常信息
            Exception Error = filterContext.Exception;
            string Message = Error.Message;//错误信息 
            string Url = filterContext.HttpContext.Request.RawUrl;//错误发生地址 
            filterContext.ExceptionHandled = true;
            Log4netHelper.Info(DateTime.Now+":"+ Url + "\n" + Message);
            filterContext.Result = new RedirectResult("/Error/Error");//跳转至错误提示页面 
        }
    }
}